package com.TetrisII.views;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;

import javax.swing.JPanel;

import com.TetrisII.entities.Shape;
import com.TetrisII.entities.ShapeFactory;
import com.TetrisII.global.Global;

/**
 * 预览面板
 * 
 * @author Administrator
 *
 */
public class PreviewPanel extends JPanel {
	private ShapeFactory shapeFactory;
	private Shape preShape;// 工厂生产的对象
	private int[] shapes;

	public PreviewPanel(ShapeFactory shapeFactory) {
		this.shapeFactory = shapeFactory;
		preShape = shapeFactory.getShape();
		shapes = preShape.getShapes()[preShape.getStatus()];
		setBackground(Color.WHITE);
		setPreferredSize(new Dimension(200, 200));
		setVisible(true);
	}

	/**
	 * 预览面板接收来自工厂的方块
	 */
	public void newShape() {
		preShape = shapeFactory.getShape();
		shapes = preShape.getShapes()[preShape.getStatus()];

	}

	/**
	 * 画出预览面板
	 */
	public void paint(Graphics g) {
		super.paintComponent(g);
		Graphics2D g_2 = (Graphics2D) g;

		URL url=PreviewPanel.class.getResource("方块.PNG");
		Image image=Toolkit.getDefaultToolkit().getImage(url);
		URL url_2=PreviewPanel.class.getResource("预览面板背景.jpg");
		Image image_2=Toolkit.getDefaultToolkit().getImage(url_2);
		g_2.drawImage(image_2, 0, 0, 200, 200, this);
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (shapes[4 * j + i] == 1) {
//					g_2.setColor(Global.shapeColor);
//					g_2.fill3DRect(40 * i + 20, 40 * j + 20, 40, 40, true);
					g_2.drawImage(image, 40 * i + 20, 40 * j + 20, 40, 40, this);
				}
			}

		}
		repaint();
	}

	/**
	 * 返回预览中的方块到游戏面板中
	 * 
	 * @return
	 */
	public Shape preShape() {
		return preShape;
	}

}
